layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 收货单位表管理
     */
    var BasicPutgoods = {
        tableId: "basicPutgoodsTable"
    };

    /**
     * 初始化表格的列
     */
    BasicPutgoods.initColumn = function () {
        return [[
            {field: 'id', title: 'Unit ID', hide: true},
            {align: 'center', width:70, toolbar: '#tableBar', title: 'Operations'},
            {field: 'putgoodsName', title: 'Unit Name', style:'font-weight: 600;',width:180},
            {field: 'unitNumber', title: 'Unit Number', style:'font-weight: 600;'},
            {field: 'setLocationId', hide: true, title: 'Address ID'},
            {field: 'setLocation', title: 'Address', style:'font-weight: 600;',width:250},
            {field: 'sheng', title: 'Province', style:'font-weight: 600;'},
            {field: 'shi', title: 'City', style:'font-weight: 600;'},
            {field: 'qu', title: 'District', style:'font-weight: 600;'},
            {field: 'setPeople', title: 'Contact Person'},
            {field: 'setTel', title: 'Phone'},
            {field: 'longitude', title: 'Longitude'},
            {field: 'latitude', title: 'Latitude'},
            {field: 'kilometer', title: 'Distance (km)'},
            {field: 'lineName', title: 'Route', hide: true},
            {field: 'putgoodsNotes', title: 'Delivery Notes'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicPutgoods.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['setLocation'] = $("#setLocation").val();
        queryData['id'] = $("#id").val();
        queryData['unitNumber'] = $("#unitNumber").val();
        table.reload(BasicPutgoods.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicPutgoods.tableId,
        url: Feng.ctxPath + '/basicPutgoods/listnew?putgoodsCustomerId='+Feng.getUrlParam("putgoodsCustomerId"),
        page: true,
        limit: 60,
		limits:[40,60,120,240,360,480,1200],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BasicPutgoods.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicPutgoods.search();
    });
    
    //回填数据
    function confirm(data){
    	parent.submit = 1;
    	parent.OrderTransportInfoDlg.data.setUnitId = data.id;//收货单位id
    	parent.OrderTransportInfoDlg.data.setUnit = data.putgoodsName;//收货单位名称
    	
    	parent.OrderTransportInfoDlg.data.setLocationId = data.setLocationId;
    	parent.OrderTransportInfoDlg.data.setLocation = data.setLocation;
    	parent.OrderTransportInfoDlg.data.setPeople = data.setPeople;
    	parent.OrderTransportInfoDlg.data.setTel = data.setTel;
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    }
    
    // 双击行事件
    table.on('rowDouble(' + BasicPutgoods.tableId + ')', function (obj) {
        var data = obj.data;
    	confirm(data);
    });

    // 工具条点击事件
    table.on('tool(' + BasicPutgoods.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'confirm') {
        	confirm(data);
        } 
    });
});
